package com.soar.recruitment.module.user.ui.fragment;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.soar.recruitment.R;
import com.soar.recruitment.common.ui.fragment.SOARBaseBackFragment;
import com.soar.recruitment.common.ui.view.MyEditView;
import com.soar.recruitment.common.ui.view.MyTextWatcher;
import com.soar.recruitment.common.ui.view.roundview.RoundTextView;
import com.soar.recruitment.common.util.SoarLoadDialog;
import com.soar.recruitment.module.user.bll.controller.RegisterContrats;
import com.soar.recruitment.module.user.bll.controller.RegisterPresenter;

import butterknife.BindView;

/**
 * 注册
 * Created by chriswen on 2017/6/16.
 */
public class RegisterFragment extends SOARBaseBackFragment implements RegisterContrats.View {

    private RegisterContrats.Presenter mPresenter;

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.tv_back)
    TextView tv_back;

    @BindView(R.id.phoneEditView)
    MyEditView phoneEditView;
    @BindView(R.id.phoneCodeEditView)
    MyEditView phoneCodeEditView;
    @BindView(R.id.passwordEditView)
    MyEditView passwordEditView;

    @BindView(R.id.rtv_register)
    RoundTextView rtv_register;

    public static RegisterFragment newInstance() {
        Bundle args = new Bundle();
        RegisterFragment fragment = new RegisterFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int initLayout() {
        return R.layout.fragment_register;
    }

    @Override
    public void initVariable() {
        mPresenter = new RegisterPresenter(this);
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        initToolbarNavBlack(toolbar);
        toolbar.setTitle("注册");

        phoneCodeEditView.setGetCodeType(true);
        setEditListener();

        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                _mActivity.onBackPressed();
            }
        });

        rtv_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPresenter.register();
            }
        });
    }

    private void setEditListener() {
        phoneEditView.addTextWatcher(new MyTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                phoneCodeEditView.setPhone(s.toString(), "1");
                phoneCodeEditView.setGetCodeStatus(!TextUtils.isEmpty(phoneEditView.getText()));
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public void showLoading(String msg) {
        SoarLoadDialog.showLoadDialog(mActivity, msg);
    }

    @Override
    public void closeLoading() {
        SoarLoadDialog.closeLoadDialog();
    }

    @Override
    public String getPhoneNum() {
        return phoneEditView.getText();
    }

    @Override
    public String getCodeNum() {
        return phoneCodeEditView.getText();
    }

    @Override
    public String getPwdNum() {
        return passwordEditView.getText();
    }

    @Override
    public void goNext() {
        // 关闭跳转到首页

    }
}
